package com.nuist.springmvcdemo.Controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

//@RestController  注意这个注解  是 ResponseBody + RestController
@RequestMapping("/res")
@Controller
public class ResponseController {

    @RequestMapping("/index")
    public String index(){
        return "/index.html";
    }
    @ResponseBody
    @RequestMapping("/indexData")
    public String indexData(){
        return "返回数据";
        // 这个 ResponseBody 表示返回数据 Controller默认表示视图
    }

    @ResponseBody
    @RequestMapping("/indexData2")
    public String indexData2(){
        return "<h1>我是中国人</h1>";
    }

    @ResponseBody
    @RequestMapping("/getMap")
    public HashMap<String,String> getMap(){
        HashMap<String,String> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(418);
        return "设置状态码";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json")
    public String setContentType(){
        return "{\"OK\":1}";
    }

}

